$(function() {
		//初始加载
		initDataGrid();
	});
	function initDataGrid() {
		//创建bootstrapTable
		$("#dataGrid").bootstrapTable({
			method:"POST",
			//极为重要，缺失无法执行queryParams，传递page参数
			contentType : "application/x-www-form-urlencoded",
			dataType:"json",
			url:'/website/member/pageInfo', 
			queryParams:queryParam,
			pagination:true,//显示分页条：页码，条数等
			striped:true,//隔行变色
			pageNumber:1,//首页页码
			pageSize:10,//分页，页面数据条数
			uniqueId:"id",//Indicate an unique identifier for each row
			sidePagination:"server",//在服务器分页
			height:tableModel.getHeight(),
			sortName:"created_at",
			toolbar:"#toolbar",//工具栏
			columns : [{
				checkbox:"true",
				field : "box"
			},  {
				title : "ID",
				field : "id",
			}, {
				title : "账户",
				field : "account"
			}, {
				title : "密码",
				field : "password"
			}, {
				title : "姓名",
				field : "name"
			},  {
				title : "创建时间",
				field : "created_at",
				sortable : true
			}],
			search : true,//搜索
            searchOnEnterKey : true,
			showRefresh : true,//刷新
            showToggle : true//

		});
	}
	function queryParam(params){
		debugger;
    	var param = {
    			limit : this.limit, // 页面大小
    	        offset : this.offset, // 页码
    	        pageNumber : this.pageNumber,
    	        pageSize : this.pageSize,
    	        sortName : this.sortName,
    	        sortOrder : this.sortOrder
    	};
    	return param;
    }
	//点击取消后清空表单中已写信息
	function resetAddModal(){
		document.getElementById("addMemberForm").reset();
	}
	//新增管理员
	function addMember(){
		var date = new Date();
		var param = $("#addMemberForm").serializeArray();
		debugger;
		$("#conf").attr("onclick","addMember()");
		$.ajax({
			url:"/website/member/addMember",
			method:"post",
			data:param,
			dataType:"json",
			success:function(data){
				if(data.state=="success"){
					document.getElementById("al").innerText="新增成功";
					document.getElementById("addMemberForm").reset();
					$("#addEnd").modal('show');
					$("#addMemberModal").modal('hide');
					$("#dataGrid").bootstrapTable('refresh');
				}
			},
			error:function(){
				document.getElementById("al").innerText="新增失败";
				$("#addEnd").modal('show');
			}
		});
	}
	//修改管理员
	function editMember(){
		//获取选中行的数据
		var rows = $("#dataGrid").bootstrapTable('getSelections');
		if(rows.length!=1){
			document.getElementById("tipContent").innerText="请选择一行数据";
			$("#Tip").modal('show');
		}
		else{
		var row = rows[0];
		$('#editId').val(row.id);
		$('#editAccount').val(row.account);
		$('#editPassword').val(row.password);
		$('#editName').val(row.name);
		$('#editCreated_at').val(row.created_at);
		$("#editMemberModal").modal("show");
		}
	}
	function updateMember(){
		var param = $("#editForm").serializeArray();
		//设为disable则无法获取
		$.ajax({
			url:"/website/member/updateMember",
			method:"post",
			data:param,
			dataType:"json",
			success:function(data){
				if(data.state=="success"){
					$("#editMemberModal").modal("hide");
//					document.getElementById("tipContent").innerText="修改成功";
//					$("#Tip").modal('show');
					toastr.success("修改成功");
					$("#dataGrid").bootstrapTable("refresh");
				}
			},
			error:function(data){
				alert("wrong");
			}
		});
	}
	//删除用户
	function deleteMember(){
			var rows = $("#dataGrid").bootstrapTable("getSelections");
			console.log(rows);
			//js中没有null，我是不是傻了，还用null来比对
			if(rows!=""){
				var ids = [];
				var len = rows.length;
				debugger;
				for(var i=0;i<len;i++){
					ids.push(rows[i].id);
				}
				debugger;
				$.ajax({
					url:"/website/member/deleteMember",
					dataType:"json",
					traditional: true,//属性在这里设置
					method:"post",
					data:{
						"ids":ids
					},
					success:function(data){
//						document.getElementById("tipContent").innerText="删除成功";
//						$("#Tip").modal('show');
						toastr.success("删除成功");
						$("#dataGrid").bootstrapTable("refresh");
					},
					error:function(){
						document.getElementById("tipContent").innerText="删除失败";
						$("#Tip").modal('show');
					}
				});
			}else{
				document.getElementById("tipContent").innerText="请选择要删除的一行";
				$("#Tip").modal('show');
			}
			
		}